466D - Increase Sequence - CodeForces Solution


combinatorics dp *2100

Please click on ads to support us..

C++ Code:

//#pragma GCC optimize("Ofast,unroll-loops") 
#include "bits/stdc++.h"
using namespace std;

#ifdef duc_debug
#include "bits/debug.h"
#else 
#define debug(...)     
#endif

#define int long long

const int maxn = 2005;
const int mod = 1e9+7;
int n, h, a[maxn];
int f[maxn][maxn]; 
// f(i, j) number of ways to
// cover [1, i] with j open

void solve(){
    cin >> n >> h;
    for(int i = 1; i <= n; ++i){
        cin >> a[i];
    }
    f[0][0] = 1;
    for(int i = 1; i <= n; ++i){
        for(int j = 0; j <= i; ++j){
            if(a[i] + j == h){ 
                if(j > 0) f[i][j] = (f[i][j] + f[i - 1][j - 1]) % mod; // add an open 
                f[i][j] = (f[i][j] + f[i - 1][j]) % mod; // do nothing
            }
            if(a[i] + j + 1 == h){
                f[i][j] = (f[i][j] + f[i - 1][j + 1] * (j + 1)) % mod; // add a close
                f[i][j] = (f[i][j] + f[i - 1][j]) % mod; // add len-1 interval
                if(j > 0) f[i][j] = (f[i][j] + f[i - 1][j] * j) % mod; // add ][ idk why
            }
            debug(i, j, f[i][j]);
        }
    }
    cout << f[n][0];
}
signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int test = 1;
    // cin >> test;
    for(int i = 1; i <= test; i++){
      // cout << "Case " << "#" << i << ": "; 
      solve();
    }

    return 0;
}


Comments

Submit
0 Comments
More Questions

1546B - AquaMoon and Stolen String
1353C - Board Moves
902A - Visiting a Friend
299B - Ksusha the Squirrel
1647D - Madoka and the Best School in Russia
1208A - XORinacci
1539B - Love Song
22B - Bargaining Table
1490B - Balanced Remainders
264A - Escape from Stones
1506A - Strange Table
456A - Laptops
855B - Marvolo Gaunt's Ring
1454A - Special Permutation
1359A - Berland Poker
459A - Pashmak and Garden
1327B - Princesses and Princes
1450F - The Struggling Contestant
1399B - Gifts Fixing
1138A - Sushi for Two
982C - Cut 'em all
931A - Friends Meeting
1594A - Consecutive Sum Riddle
1466A - Bovine Dilemma
454A - Little Pony and Crystal Mine
2A - Winner
1622B - Berland Music
1139B - Chocolates
1371A - Magical Sticks
1253A - Single Push